package com.ludomancy.spaceopera.entities;
import java.awt.Graphics;
import java.awt.Point;

import com.ludomancy.gamelib.Accumulator;
import com.ludomancy.gamelib.ImageAnimation;
import com.ludomancy.gamelib.Sound;
import com.ludomancy.gamelib.entity.EntityCollisionFilter;
import com.ludomancy.spaceopera.BasicEntity;
import com.ludomancy.spaceopera.Game;
import com.ludomancy.spaceopera.SpaceOperaApp;

public class StarBlast extends BasicEntity
{
	private ImageAnimation anim;
	private Accumulator thruster = new Accumulator(Game.forTime(1));
	private int offsetY = 1;

	public StarBlast(Star relativeTo)
	{
		super(new Point());
		z = ZFRONT;
		// position
		pos.x = relativeTo.pos.x;
		pos.y = relativeTo.pos.y;
		anim = ImageAnimation.create("blast.png", 23, 11, 2);
		anim.play();
		width = (int) proj.screenToWorldX(anim.strip.frameWidth);
		height = (int) proj.screenToWorldY(anim.strip.frameHeight);
	}
	
	public void paint(Graphics g, int x, int y)
	{
		offsetY *= -1;
		anim.update();
		anim.paintCurrentFrame(g, x, y+offsetY);
	}
	
	public boolean update()
	{
		thruster.update();
		if(thruster.isDone())
		{
			pos.x += 8;
			thruster.reset();
		}
		
		if(EntityCollisionFilter.collidesWith(this, Game.game.leviathan, proj))
		{
			// smashed the leviathan!
			// TODO: implement properly
			parent.remove(this);
			SpaceOperaApp.mainWidget.space.startShake();
			Game.game.leviathan.pos.x += Game.WORLD_WIDTH/30;
			Game.game.currentWorld.add(new Explosion(new Point(pos)));
			Sound.play("explosion.wav");
		}
		
		if(pos.x - getWidth() > Game.WORLD_WIDTH)
		{
			parent.remove(this);
		}
		return false;
	}
}
